package com.mediacloud.app.newsmodule.view;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.AbsListView;
import android.widget.ExpandableListView;
import android.widget.FrameLayout;

import com.mediacloud.app.model.news.CatalogItem;
import com.mediacloud.app.newsmodule.fragment.webview.NavigateLinkFragment;


/**
 * Created by Hoo on 2017/12/6.
 */

public class FooterWebView extends FrameLayout {

    NavigateLinkFragment fragment;
    public ExpandableListView mCatalogContentItemListView;

    public FooterWebView(@NonNull Context context) {
        this(context, null);
    }

    public FooterWebView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FooterWebView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        AbsListView.LayoutParams param = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT);
        setLayoutParams(param);
        setId(generateViewId());
    }

    public void setCatalogData(CatalogItem catalog, FragmentManager fragmentManager) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (fragment != null)
            transaction.remove(fragment);
        fragment = new NavigateLinkFragment();
        fragment.hadChoosed = true;
        transaction.replace(getId(), fragment);
        Bundle bundle = new Bundle();
        bundle.putParcelable("catalog", catalog);
        fragment.setArguments(bundle);
        transaction.show(fragment);
        transaction.commitNowAllowingStateLoss();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(mCatalogContentItemListView.getHeight(), MeasureSpec.EXACTLY);
        setMeasuredDimension(widthSize, mCatalogContentItemListView.getHeight());
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        Log.e("", "==yoake==" + event.getY());
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                if (getTop() <= 0) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                break;
            case MotionEvent.ACTION_UP:

                break;

        }
        return super.onInterceptTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }
}